import {formatMessage} from './intl';
import {request, handleFetchResultNotification, REQUEST_TYPE} from '../utils';
import * as dto from './DTO';
import stringify from 'Shared/utils/stringify';

const baseUrl = '/parts/api/v1';

const getDetailDesc = formatMessage({
    id: 'api.message.getDetailDesc',
    defaultMessage: '查询经销商信息详情'
});

const newDealerDesc = formatMessage({
    id: 'api.message.newDealerDesc',
    defaultMessage: '新增经销商'
});

const modifyDealerDesc = formatMessage({
    id: 'api.message.modifyDealerDesc',
    defaultMessage: '修改经销商信息'
});

const abandonDealerDesc = formatMessage({
    id: 'api.message.abandonDealerDesc',
    defaultMessage: '作废经销商'
});

const getListDesc = formatMessage({
    id: 'api.message.getListDesc',
    defaultMessage: '查询经销商信息'
});

// 查询经销商列表
export const getList = condition => {
    const queryString = stringify(condition);
    return request(`${baseUrl}/subDealers?${queryString}`, null, dto.List)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getListDesc));
};

// 查询详情
export const getDetail = id =>
    request(`${baseUrl}/subDealers/${id}`, null, dto.DetailDataStruct)
        .then(handleFetchResultNotification(REQUEST_TYPE.query, getDetailDesc));

//新增
export const newSubDealer = data => request(`${baseUrl}/subDealers`, {
    method: 'POST',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, newDealerDesc));
//修改
export const modifySubDealer = (id, data) => request(`${baseUrl}/subDealers/${id}`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, modifyDealerDesc));

//作废
export const abandonSubDealer = (id, data) => request(`${baseUrl}/subDealers/${id}/abandon`, {
    method: 'PUT',
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify(data)
}).then(handleFetchResultNotification(REQUEST_TYPE.submit, abandonDealerDesc));
